package org.budo.solr.jdbc.driver.statement;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.BudoJdbcResultSetMetaData;
import org.budo.jdbc.driver.statement.AbstractJdbcStatement;
import org.budo.solr.jdbc.driver.BudoSolrJdbcResultSet;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.StringUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author lmw
 */
public class SelectStatement extends AbstractJdbcStatement {
    private String fieldList;

    private String query;

    private String filter;

    private String sort;

    private Integer offset;

    private Integer limit;

    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException{
        String requestUrl = "http://localhost:8984/solr/new_core/select";
        Request request = new Request();
        request.setUrl(requestUrl);
        request.setParameter("q", "*:*");

        if (!StringUtil.isEmpty(this.getFieldList())) {
            request.setParameter("fl", this.getFieldList());
        }

        Response response = request.execute();
        String responseBody = response.getBody();

        JSONObject responseBodyJsonObject = JSON.parseObject(responseBody);
        JSONObject responseValueJsonObject = responseBodyJsonObject.getJSONObject("response");
        JSONArray docs = responseValueJsonObject.getJSONArray("docs");

        int size = docs.size();
        if (size < 1) {
            ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(new BudoSolrJdbcResultSet());
            return;
        }

        Map[] valueArray = new Map[size];
        for (int i = 0; i < size; i++) {
            valueArray[i] = docs.getJSONObject(i);
        }

        JSONObject firstDoc = docs.getJSONObject(0);
        Set<String> keySet = firstDoc.keySet();
        String[] columnNames = keySet.toArray(new String[keySet.size()]);
        BudoJdbcResultSetMetaData metaData = new BudoJdbcResultSetMetaData(columnNames);

        BudoSolrJdbcResultSet resultSet = new BudoSolrJdbcResultSet();
        resultSet.setValueArray(valueArray);
        resultSet.setMetaData(metaData);
        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }

    public String getFieldList() {
        return this.fieldList;
    }

    public String getFilter() {
        return this.filter;
    }

    public Integer getLimit() {
        return this.limit;
    }

    public Integer getOffset() {
        return this.offset;
    }

    public String getQuery() {
        return this.query;
    }

    public String getSort() {
        return this.sort;
    }

    public void setFieldList(String fieldList) {
        this.fieldList = fieldList;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }
}
